#include <bits/stdc++.h>
using namespace std;

using ll = long long;

// 前向星，基本没用

struct LinkedForwardStar
{
    struct Edge
    {
        int u;
        int v;
        int nxt;
        ll weight;
    };

    vector<Edge> edges;
    vector<int> headers;
    vector<ll> pointWeights;
    LinkedForwardStar(int n) : headers(n + 1, -1), pointWeights(n + 1) {}

    LinkedForwardStar &operator+(const tuple<int, int, ll> &e)
    {
        Edge edge;
        edge.u = get<0>(e);
        edge.v = get<1>(e);
        edge.weight = get<2>(e);
        edge.nxt = headers[edge.u];
        edges.push_back(edge);
        headers[edge.u] = edges.size() - 1;
    }

    void forEach(int u, function<void(int, ll)> iter)
    {
        for (int ne = headers[u]; ne != -1; ne = edges[ne].nxt)
            iter(edges[ne].u, edges[ne].weight);
    }
    void forEach(int u, function<void(int)> iter)
    {
        for (int ne = headers[u]; ne != -1; ne = edges[ne].nxt)
            iter(edges[ne].u);
    }
};
